package com.pm.controller.official;

import com.alibaba.fastjson.JSONObject;
import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.official.OfficialMeanDevRank;
import com.pm.entity.vo.official.OfficialBaseDataVo;
import com.pm.service.official.OfficialBaseDataService;
import com.pm.service.official.OfficialComRankService;
import com.pm.service.official.OfficialMeanDevRankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: pm_info_api
 * @description: 党政一把手基础数据
 * @author: ht
 * @create: 2020-08-19 17:43
 **/
@Api(tags = "党政一把手基础数据")
@UserLoginToken
@RestController
@RequestMapping("/OfficialBaseData")
public class OfficialBaseDataController {

    @Autowired
    OfficialBaseDataService officialBaseDataService;

    @Autowired
    OfficialComRankService officialComRankService;

    @Autowired
    OfficialMeanDevRankService officialMeanDevRankService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "党政一把手基础数据新增")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody JSONObject jsonObject) {
        Response<Integer> response = null;
        Integer result = officialBaseDataService.addBaseData(jsonObject);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "党政一把手基础数据编辑")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(@RequestBody JSONObject jsonObject) {
        Response<Integer> response = null;
        Integer result = officialBaseDataService.updateBaseData(jsonObject);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    /*@ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<OfficialBaseData>> getList(@RequestBody OfficialBaseData officialBaseData) {
        List<OfficialBaseData> result = officialBaseDataService.getList(officialBaseData);
        Response<List<OfficialBaseData>> res = new Response<List<OfficialBaseData>>(ExpCodeState.query_data_success, result);
        return res;
    }*/

    @ApiOperation("党政一把手基础数据查询接口")
    @PostMapping("/getBaseDataList")
    @ResponseBody
    public Response<List<JSONObject>> getBaseDataList(@RequestBody OfficialBaseDataVo officialBaseDataVo) {
        List<JSONObject> result = officialBaseDataService.getBaseDataList(officialBaseDataVo);
        Response<List<JSONObject>> res = new Response<List<JSONObject>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "党政一把手基础数据删除")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> remove(Integer meanDevId) {
        Response<Integer> response = null;
        OfficialMeanDevRank officialMeanDevRank = new OfficialMeanDevRank();
        officialMeanDevRank.setId(meanDevId);

        List<String> list = officialBaseDataService.getTaskIdListByMeanDevId(meanDevId);
        Integer meanDevResult = officialMeanDevRankService.removeOne(officialMeanDevRank);
        Integer result = officialBaseDataService.removeByMeanDevId(meanDevId);
        // 任务id_年份_上下半年
        officialComRankService.batchChange(list);
        if (result > 0 && meanDevResult > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }
}
